using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using NCS.Module.CMS.Library.Distributions;
using NCS.Module.CMS.Library.Definitions;
using NCS.Module.CMS.Library.Zones;
using NCS.Library.Utilities;
using NCS.Module.CMS.Library;
using NCS.Library.Components.Portal;
using NCS.Library;
using NCS.Library.Components;

namespace NCS.Module.CMS.Web.HomePage
{
    public partial class ContentList1 : CMSWebControl
    {
        public string lblPage = "";
  
        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            if (curZone == null) return;
            if (curZone.Zone_Authorize > 0)
            {
                if (CurrentUser != null)
                {
                    Boolean check = false;
                    DataTable dt = PortalController.GetPortals(CurrentUser.UserID, AppEnv.GetLanguage());
                    if (dt.Rows.Count > 0)
                    {
                        for (int i = 0; i < dt.Rows.Count; i++)
                        {
                            if (dt.Rows[i]["PortalID"].ToString() == AppEnv.PortalId())
                            {
                                check = true;
                            }
                        }
                    }
                    if (check == false)
                    {
                        MessageBox.Show(MiscUtility.MSG_CONTROKEY_CANCEL);

                        return;
                    }
                }
                else
                {
                    Session["UrlKey"] = HttpContext.Current.Request.RawUrl;
                    Response.Redirect("/");
                }
            }
            lblZoneName.Text = curZone.Zone_Name;
            int catid = 0;
            if(Request["catid"]!=null && Request["portalid"]!= null && Int32.TryParse(Request["catid"], out catid))
            {
                DataTable dtContents = ZoneController.GetZones(catid, Request["portalid"]);
                this.rpt_head.DataSource = dtContents;
                this.rpt_head.DataBind();
            }
            
        }

        protected void rpt_head_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                DataRowView currow = (DataRowView)e.Item.DataItem;
                string url_zone = BuildUrlByZone((int)currow["Zone_ID"],209);
                string src_img = ConvertUtility.ToString(currow["Zone_Avatar"]) != string.Empty ? MultimediaUtility.GetEventAvatar(currow["Zone_Avatar"].ToString()) : "";
                string datetime = "&nbsp;&nbsp;<span style='color:gray;font-size: 10px;'>" + ConvertUtility.ToDateTime(DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss tt")) + "</span>";

                Literal ltt = (Literal)e.Item.FindControl("lttImg");
                if (!string.IsNullOrEmpty(src_img) && System.IO.File.Exists(Server.MapPath(src_img)))
                {
                    ltt.Text += "<a href='" + url_zone + "' style='color:#062FAF;font-size:13px;'>";
                    ltt.Text += "<img src='" + src_img + "' alt=''   Width='150'/>";
                    ltt.Text += "</a>";
                }
                ltt.Text += "<a href='" + url_zone + "' style='color:#062FAF;font-size:13px;'>";
                ltt.Text += currow["Zone_Name"].ToString();
                ltt.Text += datetime + "</a>";
            }
        }
  
    }
}